<template>
    <div class="foot-tab-wrap">
        <van-tabbar v-model="activeIndex">
            <van-tabbar-item  v-for="(item, index) in navList" :key="index" :name="item.pathName"  @click="handleToPage(item)" :class="index === 2 ? 'cart-tab' : ''">
                <div :class="['name', activeIndex === index ? 'on' : '']">{{ item.title }}</div>
                <template #icon>
                    <img :class="['icon', activeIndex === index ? 'on' : '']" :src="activeIndex === index ? item.iconOn : item.icon" />
                </template>
            </van-tabbar-item>
        </van-tabbar>
    </div>
</template>

<script>
import {
    tabIcon1,
    tabIcon1s,
    tabIcon2,
    tabIcon2s,
    tabIcon3,
    tabIcon3s,
    tabIcon4,
    tabIcon4s,
    tabIcon5,
    tabIcon5s
} from '@/tool/static-img';
export default {
    props: {
        current: {
            type: Number,
            default: 0,
        }
    },
    data() {
        return {
            active:'home',
            activeIndex: this.current,
            navList: [{
                title: '首页',
                pathName: 'home',
                icon: tabIcon1,
                iconOn: tabIcon1s
            },
            {
                title: '本地生活',
                pathName: 'store',
                icon: tabIcon5,
                iconOn: tabIcon5s
            },
            {
                pathName: 'cart',
                icon: tabIcon3,
                iconOn: tabIcon3
            },
            {
                title: '品牌馆',
                pathName: 'classify',
                icon: tabIcon2,
                iconOn: tabIcon2s
            },
            {
                title: '我的',
                pathName: 'mine',
                icon: tabIcon4,
                iconOn: tabIcon4s
            }]
        }
    },
    methods: {
        handleToPage(item) {
            const index = this.navList.findIndex(nav => nav.pathName === item.pathName);
            this.activeIndex = index;
            this.$router.replace({
                name: item.pathName
            })
        },
        // 根据路由路径更新activeIndex
        updateActiveIndex() {
            const currentRoute = this.$route.name;
            const index = this.navList.findIndex(item => item.pathName === currentRoute);
            if (index !== -1) {
                this.activeIndex = index;
            }
        }
    },
    watch: {
        // 监听路由变化
        '$route': {
            handler() {
                this.updateActiveIndex();
            },
            immediate: true
        },
        // 监听props变化
        current(newVal) {
            this.activeIndex = newVal;
        }
    }

}
</script>

<style lang="scss">
.foot-tab-wrap {
    position: fixed;
    bottom: 0;
    left: 0;
    z-index: 9999 !important;
    display: flex;
    align-items: center;
    width: 100%;
    padding-bottom: calc(constant(safe-area-inset-bottom));
    padding-bottom: calc(env(safe-area-inset-bottom));
    border-top: 1px solid #E3E3E3;
    background-color: #ffffff;
    box-sizing: content-box;

    .van-tabbar {
        height: 0.96rem;
    }

    .van-tabbar-item {
        padding-top: 0.05rem;
    }

    .van-tabbar-item__icon {
        margin-bottom: 0.03rem;
    }

    /* 购物车Tab的绿色圆形效果 */
    .cart-tab {
        position: relative;
        margin-top: -0.3rem;
        z-index: 1;

        .van-tabbar-item__icon {
            background-color: #fff;
            border-radius: 50%;
            width: 1.15rem;
            height: 1.15rem;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 -2px 8px rgba(49, 166, 83, 0.3);
            position: relative;
            z-index: 2;
    
        }

        .icon {
           width: 1.12rem;
            height: 1.12rem;
            margin-top: 0.03rem;
            &.on {
            width: 1.12rem;
            height: 1.12rem;
            }
        }
    }

    .tab-li {
        display: flex;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        flex: 1;
        padding: 0.06rem 0;



    }
    .icon {
            width: 0.48rem;
            height: 0.48rem;
            margin-bottom: 0.03rem;
            &.on {
                width: 0.56rem;
                height: 0.56rem;
            }
        }
    .name {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 0.24rem;
            line-height: 0.34rem;
            color: #999;

            &.on {
                font-weight: 500;
                font-size: .28rem;
                color: #31A653;
            }
        }
}
</style>
